package proc

import (
	"github.com/hu17889/go_spider/core/common/page"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"log"
	"net/http"
	"strconv"
	"tesou.io/platform/poem-parent/poem-api/common/base"
	"tesou.io/platform/poem-parent/poem-api/module/core/enums"
	"tesou.io/platform/poem-parent/poem-api/module/core/pojo"
	"tesou.io/platform/poem-parent/poem-core/module/core/service"
)

type TieBaProcesser struct {
	zhihuService    service.TieBaService
	SpiderUrl string
	cookies []*http.Cookie
}

func GetTieBaProcesser() *TieBaProcesser {
	return &TieBaProcesser{}
}

func (this *TieBaProcesser) Startup() {
	newSpider := spider.NewSpider(this, "TieBaProcesser")

	//newSpider = newSpider.AddUrl(this.SpiderUrl, "html")
	base.Log.Info("start URL:,", this.SpiderUrl)
	//hder := make(http.Header)
	//hder.Set("Cookie", `_zap=09ee8132-fd2b-43d3-9562-9d53a41a4ef5; d_c0="AGDv-acVoQ-PTvS01pG8OiR9v_9niR11ukg=|1561288241"; capsion_ticket="2|1:0|10:1561288248|14:capsion_ticket|44:NjE1ZTMxMjcxYjlhNGJkMjk5OGU4NTRlNDdkZTJhNzk=|7aefc35b3dfd27b74a087dd1d15e7a6bb9bf5c6cdbe8471bc20008feb67e7a9f"; z_c0="2|1:0|10:1561288250|4:z_c0|92:Mi4xeGZsekFBQUFBQUFBWU9fNXB4V2hEeVlBQUFCZ0FsVk5PcXo4WFFBNWFFRnhYX2h0ZFZpWTQ5T3dDMGh5ZTV1bjB3|0cee5ae41ff7053a1e39d96df2450077d37cc9924b337584cf006028b0a02f30"; q_c1=ae65e92b2bbf49e58dee5b2b29e1ffb3|1561288383000|1561288383000; tgw_l7_route=f2979fdd289e2265b2f12e4f4a478330; _xsrf=f8139fd6-b026-4f01-b860-fe219aa63543; tst=h; tshl=`)
	//hder.Set("User-Agent", `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Mobile Safari/537.36`)
	//req := corerequest.NewRequest(this.SpiderUrl, "html", "hotindex", "GET", "", hder, nil, nil,nil)
	//newSpider.AddRequest(req)


	newSpider.AddUrl(this.SpiderUrl, "json")

	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetThreadnum(1).Run()
}

func (this *TieBaProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Info("URL:,", request.Url, p.Errormsg())
		return
	}
	query := p.GetJson()
	status, err := query.GetPath("errno").Int()
	if status != 0 || err != nil {
		log.Panicf("page is crawled error : errorinfo=%s : status=%d : startNewsId=%d", err.Error(), status, 1)
	}

	//var allData []map[string]interface{}
	zhi_list_slice := make([]interface{}, 0)
	zhi_list_update_slice := make([]interface{}, 0)
	level := enums.GetListLevel("贴吧")

	query = query.Get("data").Get("bang_topic").Get("topic_list")
	for i := 0; i < 30; i++ {
		ListId, err := query.GetIndex(i).Get("topic_id").Int()
		if ListId <1 || err != nil {
			continue
		}
		Title, err := query.GetIndex(i).Get("topic_desc").String()
		SUrl, err := query.GetIndex(i).Get("topic_url").String()
		Image, err := query.GetIndex(i).Get("topic_pic").String()

		zhi := new(pojo.List)
		zhi.ListId = strconv.Itoa(ListId)
		zhi.Title = Title
		zhi.SUrl = SUrl
		zhi.Image = Image
		zhi.Content = ""
		if len(zhi.Title) > 0 {
			//设置朝代
			zhi.Type = level
			exist := this.zhihuService.Exist(zhi)
			if !exist {
				zhi_list_slice = append(zhi_list_slice, zhi)
			} else {
				zhi_list_update_slice = append(zhi_list_update_slice, zhi)
			}
		}
	}

	//执行入库
	this.zhihuService.SaveList(zhi_list_slice)
	this.zhihuService.ModifyList(zhi_list_update_slice)

}

func (this *TieBaProcesser) Finish() {
	base.Log.Info("zhihu表抓取解析完成 \r\n")

}

